﻿@model IEnumerable<AdventureWorks.Entities.Product>
@using PagedList;
@using PagedList.Mvc;
@using AdventureWorks.Entities;
@using AdventureWorks.Business;
@{
	ViewBag.Title = "Product List";
	int pageIndex = ViewBag.PageIndex;
	int pageSize = ViewBag.PageSize;

	ProductFilter filter = new ProductFilter
	{
		SortOrder = ViewBag.NameSortParam,
		SearchString = ViewBag.CurrentFilter,
		ProductCategoryID = ViewBag.productCategoryID,
		ProductModelID = ViewBag.productModelID
	};
}
<link href="/Content/PagedList.css" rel="stylesheet" type="text/css" />

<table id="producstList">
    <tr>
		<th>
			@SearchLink("Name", "Search", filter)
		</th>
        <th>Number</th>
        <th>Color</th>
		<th>
			@{filter.SortOrder = ViewBag.CostSortParam;}
			@SearchLink("Cost", "Search", filter)
		</th>
		<th>
			@{filter.SortOrder = ViewBag.PriceSortParam;}
			@SearchLink("Price", "Search", filter)
		</th>
        <th>Size</th>
		<th>
			@{filter.SortOrder = ViewBag.WeightSortParam;}
			@SearchLink("Weight", "Search", filter)
		</th>
        <th>Category</th>
        <th>Model</th>
		<th>Image</th>
        <th>Sell Start</th>
        <th>Sell End</th>
		<th>
			@{filter.SortOrder = ViewBag.ModifiedSortParam;}
			@SearchLink("Modified", "Search", filter)
		</th>
        <th></th>
    </tr>

@foreach (var item in Model.ToPagedList<Product>(pageIndex, pageSize))
{
    <tr>
        <td>@Html.DisplayFor(modelItem => item.Name)</td>
        <td>@Html.DisplayFor(modelItem => item.ProductNumber)</td>
        <td>@Html.DisplayFor(modelItem => item.Color)</td>
        <td>@Html.DisplayFor(modelItem => item.StandardCost)</td>
        <td>@Html.DisplayFor(modelItem => item.ListPrice)</td>
        <td>@Html.DisplayFor(modelItem => item.Size)</td>
        <td>@Html.DisplayFor(modelItem => item.Weight)</td>
        <td>@Html.DisplayFor(modelItem => item.ProductCategory.Name)</td>
		<td>@Html.DisplayFor(modelItem => item.ProductModel.Name)</td>
		<td><img alt="@item.ThumbnailPhotoFileName" src="@Url.Action("Image", new { id = item.ID })" width="80" /></td>
        <td>@Html.DisplayFor(modelItem => item.SellStartDate)</td>
        <td>@Html.DisplayFor(modelItem => item.SellEndDate)</td>
        <td>@Html.DisplayFor(modelItem => item.ModifiedDate)</td>
        <td>
            @Html.ActionLink("Edit", "Edit", new
			{
				id = item.ID,
				ddlProductCategoryID = ViewBag.productCategoryID,
				ddlProductModelID = ViewBag.productModelID
			}) |
            
			@Html.ActionLink("Details", "Details", new
			{
				id = item.ID,
				ddlProductCategoryID = ViewBag.productCategoryID,
				ddlProductModelID = ViewBag.productModelID
			}) |
            
			@Html.ActionLink("Delete", "Delete", new 
			{ 
				id = item.ID,
				ddlProductCategoryID = ViewBag.productCategoryID,
				ddlProductModelID = ViewBag.productModelID
			})
        </td>
    </tr>
}
	<tr>
		<td colspan="7">
			@Html.PagedListPager(
			(IPagedList)Model.ToPagedList(pageIndex, pageSize),
			page => Url.Action("Index", new 
			{ 
				ddlProductCategoryID = ViewBag.productCategoryID,
				ddlProductModelID = ViewBag.productModelID,
				page, sortOrder = ViewBag.CurrentSort,
				searchString = ViewBag.SearchString 
			}),
			new PagedListRenderOptions { 
	                           			LinkToFirstPageFormat = "<< First",
	                           			LinkToPreviousPageFormat = "< Prev",
	                           			LinkToNextPageFormat = "> Next",
	                           			LinkToLastPageFormat = ">> Last",
	                           			MaximumPageNumbersToDisplay = 5
									   })
		</td>
	</tr>

</table>

@helper SearchLink(String linkText, String actionName, ProductFilter filter)
	{
	@Ajax.ActionLink(linkText, actionName, new
	{
		sortOrder = filter.SortOrder,
		currentFilter = filter.SearchString,
		ddlProductCategoryID = filter.ProductCategoryID,
		ddlProductModelID = filter.ProductModelID
	}, new AjaxOptions{
		UpdateTargetId = "producstList",
	})
}
